package com.wefans.wefansapp.activity;

import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wefans.wefansapp.R;
import com.wefans.wefansapp.common.tool.data.ResTermsData;
import com.wefans.wefansapp.common.tool.debug.MFDebug;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager.IOnNetworkListener;
import com.wefans.wefansapp.common.tool.network.model.NetJsonData;
import com.wefans.wefansapp.common.tool.network.model.NetMessage;
import com.wefans.wefansapp.common.tool.utils.MFToastUtil;

public class TermsActivity extends BaseActivity {

	private Context mContext = null;
	private TextView mTvContents = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		mContext = this;
		
		setContentView(R.layout.activity_terms);
		
		mTvContents = (TextView)findViewById(R.id.TV_CONTENTS);
		
		requestTerms();
	}
	
	private void requestTerms() {
		String strUrl = "http://api.wefansapp.com/app/get_info.php";
		MFNetworkManager.getInstance(this).requestGetData(MFNetworkManager.REQUEST_TERMS, strUrl, mNetworkListener);
	}
	
	IOnNetworkListener mNetworkListener = new IOnNetworkListener() {
		
		@Override
		public void onNetMessage(int resultCode, NetMessage message) {
			try {
	            //서버 여동 데이터
	            if( message instanceof NetJsonData ) {
	                long magicNum = ((NetJsonData) message).getMagicNum();
	                String json = ((NetJsonData) message).getResponseJson();
	                
	                if( json == null ) {
	                    throw new Exception("invalid json data");
	                }
	                
	                Gson gson = new Gson();
	                
	                ResTermsData resultData = (ResTermsData)gson.fromJson(json, ResTermsData.class);
                    if( resultData == null ) {
                        throw new Exception("Gson parsing error");
                    }
	                
	                if (magicNum == MFNetworkManager.REQUEST_TERMS) {
	                	if (resultData.isSuccsess()) {
	                		mTvContents.setText(resultData.getContent());
	                	} else {
	                		MFToastUtil.NetResponseToast(getApplicationContext(), resultData.getResultcode(), resultData.getContent());
	                	}
	                }
	            }	            
	        } catch(Exception e) {
	            MFDebug.e(e);
	        }		
			
		}
	};
}
